// (c) 2015 Teresa Holfeld
package com.holfeld.teresa.vocabtrainer.card;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.holfeld.teresa.vocabtrainer.R;
import com.holfeld.teresa.vocabtrainer.provider.VocabularyPool;

/**
 * @author Teresa Holfeld, teresa.holfeld@gmail.com
 */
public class AddVocabularyCard extends CardView {

    private String mVocabularyName;

    private ButtonCallback mButtonCallback;

    public AddVocabularyCard(Context context) {
        super(context);
        initCallback(context);
        init();
    }

    public AddVocabularyCard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCallback(context);
        init();
    }

    public AddVocabularyCard(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCallback(context);
        init();
    }

    private void initCallback(Context context) {
        // Activities containing this fragment must implement its callbacks.
        if (!(context instanceof ButtonCallback)) {
            throw new IllegalStateException("Activity must implement view's callback.");
        }
        mButtonCallback = (ButtonCallback) context;
    }

    private void init() {
        inflate(getContext(), R.layout.card_add_vocabulary, this);
        final TextView title = (TextView) findViewById(R.id.title);
        final TextView locale1 = (TextView) findViewById(R.id.locale1);
        final TextView locale2 = (TextView) findViewById(R.id.locale2);
        Button saveButton = (Button) findViewById(R.id.save);
        saveButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((View) v.getParent()).setVisibility(GONE);
                saveVocabulary(title, locale1, locale2);
                mButtonCallback.onSaveVocabulary(AddVocabularyCard.this);
            }
        });
        Button cancelButton = (Button) findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mButtonCallback.onCancelVocabulary(AddVocabularyCard.this);
            }
        });
        CardTextWatcher textWatcher =
                new CardTextWatcher(saveButton, title, locale1, locale2);
        title.addTextChangedListener(textWatcher);
        locale1.addTextChangedListener(textWatcher);
        locale2.addTextChangedListener(textWatcher);
        saveButton.setEnabled(false);
    }

    public String getVocabularyName() {
        return mVocabularyName;
    }

    private void saveVocabulary(TextView titleInput, TextView locale1Input, TextView locale2Input) {
        String name = titleInput.getText().toString();
        String locale1 = locale1Input.getText().toString();
        String locale2 = locale2Input.getText().toString();
        VocabularyPool.getInstance(getContext().getApplicationContext())
                .addVocabulary(name, locale1, locale2);
        mVocabularyName = name;
    }

    /**
     * A callback interface that all activities containing this fragment must
     * implement. This mechanism allows activities to be notified of save actions, and define their
     * own behavior accordingly (e.g. adding a AddVocableCard to the parent layout).
     */
    public interface ButtonCallback {
        public void onSaveVocabulary(AddVocabularyCard addVocabularyCard);
        public void onCancelVocabulary(AddVocabularyCard addVocabularyCard);
    }
}
